const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
function setIdNumber(data) {
  return new Promise((resolve, reject) => {
    // 获取缓存数据
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        let idList = res.data || []; // 如果不存在缓存数据，设置为空数组
        idList.push(data); 
        console.log(idList)
        // 设置缓存数据
        wx.setStorage({
          key: 'userInfo',
          data: idList,
          success: function() {
            console.log('设置缓存数据成功');
            resolve(); // 成功时解析 Promise
          },
          fail: function(error) {
            console.error('设置缓存数据失败:', error);
            reject(error); // 失败时拒绝 Promise
          }
        });
      },
      fail: function(error) {  
        reject(error); // 失败时拒绝 Promise
      }
    });
  });
}

function init(){
  wx.setStorage({
    key: 'userInfo',
    data: [],
    success: function() {
      console.log('初始化缓存数据成功');
    },
    fail: function(error) {
      console.error('初始化缓存数据失败:', error);
    }
  }); 
}
function getIdNumber() {
  return new Promise(function(resolve, reject) {
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        resolve(res.data);
      },
      fail: function(error) {
        reject(error);
      }
    });
  });
}



module.exports = {
  formatTime,
  setIdNumber,
  getIdNumber,
  init
}